#include "StudentRegister.h"
#include<ostream>
#include<sstream>
#include<fstream>



StudentRegister::StudentRegister()
{
	list="";
}

void StudentRegister::Add(Student s)
{
	v.push_back(s);
}


string StudentRegister::List()
{

	ostringstream s;
	cout << "List:" << endl;
	s << list << endl;
	
	for (int i = 0; i < v.size(); i++)
	{
		s << v[i] << endl;
	}

	return s.str();
	
}

void StudentRegister::WritetoFile(string filename)
{
	ofstream fout(filename);
	
	fout<< "Student Register= " << list << endl;
	
	for(auto &s: v)
	{
		fout << "Student= "<< s << endl;
	}
	fout.close();
}

void StudentRegister::ReadfromFile(string filename)
{
	ifstream fin(filename);
	string str, strRow;
	try
	{
		
		v.clear();
		while(getline(fin, strRow))
		{
			
			istringstream is(strRow);
			getline(is, str, '=');
			if(str== "Student Register")
			{
				getline(is, this->list);
			}
			else if(str.compare("Student")==0)
			{
				Student st;
				is >> st;
				v.push_back(st);
			}

		}
		fin.close();
	}
	catch(exception e)
	{
		cerr <<"Exeption: " << filename << endl;
		cerr << e.what() << endl;
	}
}